package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import controller.UI;

/**
 * Logger window:
 * 		two tables - logger and statistics
 * 		edit options - logger display (iteractive/static), logger history, sorting
 */
public class LoggerFrame extends JFrame
{
	/* Current shown window */
	public static LoggerFrame currentloggerFrame = null;

	/* Logger panel */
	private LoggerPanel logTable = null;
	
	/* Previous history depth */
	private int depth = Settings.historyDepth;
	
	/* Previous filter options */
	private boolean filterOptionNeeded = Settings.filterOptionNeeded;
	private String filterOption = Settings.filterOption;
	private boolean filterParameterNeeded = Settings.filterParameterNeeded;
	private String filterParameter = Settings.filterParameter;
	
	/* C'tor */
	private LoggerFrame()
	{	
		super("Logger");
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(new Dimension(800,550));
		setLayout(new BorderLayout());
		
		addStandartButtons();
		addLoggerButtons();
		addLoggerTable();
		
		currentloggerFrame = this;
	}
	
	public static LoggerFrame getInctance()
	{
		if(currentloggerFrame == null)
		{
			return new LoggerFrame();
		}
		
		return currentloggerFrame;
	}
	
	/**
	 * 
	 */
	private void addLoggerButtons() 
	{
		JPanel buttons = new JPanel();
		buttons.setLayout(new GridLayout(1,3));
	    add(buttons, BorderLayout.NORTH);
	    
	    
		/* Buttons for mode set */
		Box modeBox = Box.createVerticalBox();
		
		final JButton staticMode = new JButton("Static Mode    ");
		staticMode.setToolTipText("Logger displays last received data");
		staticMode.setEnabled(false);
		final JButton dynamicMode = new JButton("Dynamic Mode");
		dynamicMode.setToolTipText("Logger dynamically displays new data");
		
		modeBox.add(staticMode);
		modeBox.add(dynamicMode);
		buttons.add(modeBox);
	
		Box optionsBox = Box.createVerticalBox();
		
		/* Buttons for filter settings */		
		JButton sort = new JButton("Filter              ");
		sort.setToolTipText("Filter the logger reports by chosen parameter");
		JButton depth = new JButton("History Depth ");
		depth.setToolTipText("Set the depth of the logger reports");

		
		optionsBox.add(depth);
		optionsBox.add(sort);
		
		buttons.add(optionsBox);
		
		/* Buttons for reset */
		JButton reset = new JButton("Reset               ");
		reset.setToolTipText("Set the default settings");
		Box resetBox = Box.createVerticalBox();
		resetBox.add(reset);
		buttons.add(resetBox);
		
		/* Set the default settings */
		reset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Settings.historyDepth = Settings.DEFAULT_LOGGER_HISTORY_DEPTH;
				Settings.filterOptionNeeded = false;
				
				LoggerTable.loggerData = UI.getLoggerSessionsReports(Settings.historyDepth);
				LoggerTable.currentLoggerTable.build();
			}	  
		});
		
		/* Set static mode */
		staticMode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				staticMode.setEnabled(false);
				dynamicMode.setEnabled(true);
				
				LoggerDynamicAction.stop();
			}	  
		});
		
		/* Set dynamic mode */
		dynamicMode.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				staticMode.setEnabled(true);
				dynamicMode.setEnabled(false);
				
				new LoggerDynamicAction();
			}	  
		});
	
		/* Open filter window by click */
		sort.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerFilterFrame msg = new LoggerFilterFrame();
				msg.setVisible(true);
			}	  
		});	
		
		/* Open depth window by click */
		depth.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				LoggerDepthFrame msg = new LoggerDepthFrame();
				msg.setVisible(true);
			}	  
		});
	}
	
	/**
	 * Adds cancel, finish, help buttons.
	 * Builds their listeners.
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");
		JButton help = new JButton("Help");
		cancel.setToolTipText("Help for logger screen");
		
		tableBox.add(help);		
		tableBox.add(finish);
		tableBox.add(cancel);
	    
		
		/*Help button listener - closes the frame*/		
		help.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				//TODO: call Maya function		
			}	  
		});	
		
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				if(LoggerDynamicAction.timer != null)
				{
					LoggerDynamicAction.stop();
				}
				LoggerFrame.currentloggerFrame.dispose();				
			}	  
		});
		  
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Settings.historyDepth = depth;
				Settings.filterOptionNeeded = filterOptionNeeded;
				Settings.filterOption = filterOption;
				Settings.filterParameterNeeded = filterParameterNeeded;
				Settings.filterParameter = filterParameter;
				
				if(LoggerDynamicAction.timer != null)
				{
					LoggerDynamicAction.stop();
				}
				LoggerFrame.currentloggerFrame.dispose();			
			}	  
		});

		/*
		addWindowStateListener(new WindowStateListener(){
			public void windowStateChanged(WindowEvent event)
			{
				if(event.getNewState() == WindowEvent.WINDOW_CLOSING)
				{
					frameClose();
				}
			}
		});
		*/
	    add(tableBox, BorderLayout.SOUTH);
	}

	/**
	 * Adds table for logger data.
	 */
	private void addLoggerTable()
	{
		JPanel tables = new JPanel();
		tables.setLayout(new BorderLayout());
		
		logTable = new LoggerPanel();
		tables.add(logTable, BorderLayout.NORTH);
		
		StatisticsPanel statTable = new StatisticsPanel();		
		tables.add(statTable, BorderLayout.SOUTH);
		
		add(tables, BorderLayout.CENTER);
	}
	
	private void frameClose()
	{
		Settings.historyDepth = depth;
		Settings.filterOptionNeeded = filterOptionNeeded;
		Settings.filterOption = filterOption;
		Settings.filterParameterNeeded = filterParameterNeeded;
		Settings.filterParameter = filterParameter;
		
		if(LoggerDynamicAction.timer != null)
		{
			LoggerDynamicAction.stop();
		}
		
		LoggerFrame.currentloggerFrame.dispose();
	}

}
